// subChat/startVoting/startVoting.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    result: [], //多选
    radio: null, //单选
    voteInfo: null,
    voteOption: null,
    chooseId: null
  },
  //选中
  onChange(event) {
    this.setData({
      result: event.detail,
    });
  },
  // 点击选项的方式
  onChangeByClick(e) {
    const value = e.currentTarget.dataset.value + ''
    const index = this.data.result.findIndex((item) => {
      return item == value
    });
    if (index !== -1) {
      this.data.result.splice(index, 1)
      this.setData({
        result: this.data.result
      })
      return
    }
    this.setData({
      result: [...this.data.result, value],
    });
    return false
  },
  //单选
  onRadioByClick(e) {
    console.log(123);
    const value = e.currentTarget.dataset.value
    if (this.data.radio == value) {
      this.setData({
        radio: ''
      })
      return
    }
    this.setData({
      radio: value
    })
  },
  // 获取投票的信息
  async getVoteInfo() {
    wx.showLoading({
      title: '加载中',
    })
    try {
      const json = await getApp().$axios("/getVoteInfo", "POST", {
        vid: this.data.id,
        uid: getApp().globalData.userInfo.id
      })
      wx.hideLoading()
      if (json.status !== 200) {
        wx.showToast({
          title: '服务器出问题了',
          icon: 'none'
        })
        return
      }
      const {
        voteInfo,
        voteOption,
        chooseId
      } = json.data
      this.setData({
        voteInfo,
        voteOption,
        chooseId
      })
    } catch (error) {
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
    }
  },
  // 确认选中
  async comfirm() {
    // 判断是否多选
    if (this.data.voteInfo && this.data.voteInfo.type == 1) {
      const uid = getApp().globalData.userInfo.id;
      try {
        const json = await getApp().$axios("/addVoteMessage", "POST", {
          uid,
          ids: this.data.result,
          vid: this.data.id,
        })
        if (json.status !== 200) {
          wx.showToast({
            title: '异常',
            icon: 'none'
          })
          return
        }
        this.getVoteInfo()
      } catch (error) {
        wx.showToast({
          title: error.msg,
          icon: 'error'
        })
      }
    } else {
      const uid = getApp().globalData.userInfo.id;
      try {
        const json = await getApp().$axios("/addVoteMessage", "POST", {
          uid,
          ids:[ this.data.radio],
          vid: this.data.id,
        })
        if (json.status !== 200) {
          wx.showToast({
            title: '异常',
            icon: 'none'
          })
          return
        }
        this.getVoteInfo()
      } catch (error) {
        wx.showToast({
          title: error.msg,
          icon: 'error'
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    const id = options.id
    this.setData({
      id
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getVoteInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})